/**
 * QualityController.java
 * Created on  2015/4/1 15:51
 * 版本       修改时间         作者        修改内容
 * V1.0.1     2015/4/1     panzhuowen    初始版本
 *
 */
package com.infitecs.eyas.quality;

import com.infitecs.eyas.constant.Constant;
import com.infitecs.eyas.judge.service.JudgeService;
import com.infitecs.eyas.power.dao.OperateFieldDao;
import com.infitecs.eyas.power.dao.PowerDao;
import com.infitecs.eyas.power.model.OperateField;
import com.infitecs.eyas.power.model.Power;
import com.infitecs.eyas.quality.dao.AshDao;
import com.infitecs.eyas.quality.dao.BurstDao;
import com.infitecs.eyas.quality.dao.ElementDao;
import com.infitecs.eyas.quality.dao.IndustryDao;
import com.infitecs.eyas.quality.model.Ash;
import com.infitecs.eyas.quality.model.Burst;
import com.infitecs.eyas.quality.model.Element;
import com.infitecs.eyas.quality.model.Industry;
import com.infitecs.eyas.step.service.StepService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * 类的描述信息
 *
 * @author panzhuowen
 * @version 1.0.1
 */
@Controller
@RequestMapping("/quality/")
public class QualityController {

    @Autowired
    private JudgeService judgeService;

    @Autowired
    private StepService stepService;
    @Autowired
    private PowerDao powerDao;
    @Autowired
    private IndustryDao industryDao;
    @Autowired
    private ElementDao elementDao;
    @Autowired
    private AshDao ashDao;
    @Autowired
    private BurstDao burstDao;
    @Autowired
    private OperateFieldDao operateFieldDao;

    @RequestMapping("industry")
    public String industry(HttpSession session, HttpServletRequest request) {
        String cId = (String) session.getAttribute("caseId");
        if (cId == null) {
            request.setAttribute("recover", false);
            return "quality/industry";
        }
        int caseId = Integer.parseInt(cId);
        Power powerTitle = powerDao.getCaseById(cId);
        Industry industry = industryDao.getIndustryByPowerCaseId(caseId);
        OperateField operateField = operateFieldDao.getOperateField(caseId);
        request.setAttribute("powerTitle", powerTitle);
        request.setAttribute("industry", industry);
        request.setAttribute("caseId", caseId);
        request.setAttribute("operateField", operateField);
        request.setAttribute("message", request.getParameter("message"));
        return "quality/industry";
    }

    @RequestMapping("element")
    public String element(HttpSession session, HttpServletRequest request) {
        String cId = (String) session.getAttribute("caseId");
        if (cId == null) {
            return "quality/element";
        }
        int caseId = Integer.parseInt(cId);
        int step = stepService.getMzFxStep(caseId);
        //判断当前方案流程到哪里
        if (step < 2) {
            request.setAttribute("step", step);
            return "quality/element";
        }
        Power powerTitle = powerDao.getCaseById(cId);
        OperateField operateField = operateFieldDao.getOperateField(caseId);
        Element element = elementDao.getElementByPowerCaseId(caseId);
        request.setAttribute("powerTitle", powerTitle);
        request.setAttribute("element", element);
        request.setAttribute("caseId", caseId);
        request.setAttribute("operateField", operateField);
        request.setAttribute("message", request.getParameter("message"));
        return "quality/element";
    }

    @RequestMapping("grey")
    public String grey(HttpSession session, HttpServletRequest request) {
        String cId = (String) session.getAttribute("caseId");
        if (cId == null) {
            return "quality/grey";
        }
        int caseId = Integer.parseInt(cId);
        int step=stepService.getMzFxStep(caseId);
        //判断当前方案流程到哪里
        if (step < 3) {
            request.setAttribute("step", step);
            return "quality/grey";
        }
        Power powerTitle = powerDao.getCaseById(cId);
        OperateField operateField = operateFieldDao.getOperateField(caseId);
        Ash ash = ashDao.getAshByPowerCaseId(caseId);
        request.setAttribute("powerTitle", powerTitle);
        request.setAttribute("ash", ash);
        request.setAttribute("caseId", caseId);
        request.setAttribute("operateField", operateField);
        request.setAttribute("message", request.getParameter("message"));
        return "quality/grey";
    }

    @RequestMapping("boom")
    public String boom(HttpSession session, HttpServletRequest request) {
        String cId = (String) session.getAttribute("caseId");
        if (cId == null) {
            return "quality/boom";
        }
        int caseId = Integer.parseInt(cId);
        int step = stepService.getMzFxStep(caseId);
        //判断当前方案流程到哪里
        if (step < 4) {
            request.setAttribute("step", step);
            return "quality/boom";
        }
        Power powerTitle = powerDao.getCaseById(cId);
        OperateField operateField = operateFieldDao.getOperateField(caseId);
        Burst burst = burstDao.getBurstByPowerCaseId(caseId);
        request.setAttribute("powerTitle", powerTitle);
        request.setAttribute("burst", burst);
        request.setAttribute("caseId", caseId);
        request.setAttribute("operateField", operateField);
        request.setAttribute("message", request.getParameter("message"));
        return "quality/boom";
    }

    @RequestMapping("report")
    public String report(HttpSession httpSession, Model model) {
        int caseId = Integer.parseInt(httpSession.getAttribute("caseId").toString());
        model.addAttribute("reportList", judgeService.getReportByCaseId(caseId));
        return "quality/report";
    }
}
